package com.xju.ExampleSystem;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class ExampleFrame extends JFrame implements ActionListener {

	private JButton buttonOK;
	private JTextArea jTextArea;
	private JRadioButton buttonA, buttonB, buttonC, buttonD;
	private Example example;
	private JLabel jLabel;
	private ButtonGroup buttonGroup;

	@Override
	public void actionPerformed(ActionEvent evnt) {

		if (evnt.getSource().equals(buttonOK)) {
			try {
				if (example.isHasNext()) {
					Ask();
					refreshDisplayMessage();
				} else {
					JOptionPane.showMessageDialog(this,
							"您的成绩为" + example.getScore(), "考试结束",
							JOptionPane.OK_OPTION);
				}
			} catch (Exception e) {
				JOptionPane.showMessageDialog(this, e.getMessage(), "错误",
						JOptionPane.OK_OPTION);
			}

		}
	}

	private void Ask() {
		if (buttonA.isSelected())
			example.Asks("A");
		if (buttonB.isSelected())
			example.Asks("B");
		if (buttonC.isSelected())
			example.Asks("C");
		if (buttonD.isSelected())
			example.Asks("D");

	}

	private void refreshDisplayMessage() {
		Question question = example.getCurrentQuestion();
		buttonA.setText(question.getAnswerA());
		buttonB.setText(question.getAnswerB());
		buttonC.setText(question.getAnswerC());
		buttonD.setText(question.getAnswerD());
		jTextArea.setText(question.getContent());
		buttonA.setSelected(false);
		buttonB.setSelected(false);
		buttonC.setSelected(false);
		buttonD.setSelected(false);
		jLabel.setText("总共有" + example.getQuestionCount() + "个题目\n" + "当前做的是第"
				+ (example.getCurrent() + 1) + "个.");
	}

	public ExampleFrame() {
		super();
		init();
	}

	private void setActionListenerForThis(JButton component) {
		component.addActionListener(this);
		this.add(component, null);
	}

	private void setActionListenerForThis(JRadioButton component) {
		component.addActionListener(this);
		this.add(component, null);
	}

	private void init() {
		setSize(600, 400);
		this.getContentPane().setLayout(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setName("考试系统");
		this.setTitle("考试系统");
		buttonGroup = new ButtonGroup();
		jLabel = new JLabel();
		jLabel.setBounds(200, 320, 500, 40);
		buttonOK = new JButton("确认");
		buttonOK.setBounds(10, 260, 120, 20);

		buttonA = new JRadioButton("A");
		buttonA.setBounds(10, 40, 200, 20);
		buttonB = new JRadioButton("B");
		buttonB.setBounds(10, 100, 200, 20);
		buttonC = new JRadioButton("C");
		buttonC.setBounds(10, 160, 200, 20);
		buttonD = new JRadioButton("D");
		buttonD.setBounds(10, 220, 200, 20);

		jTextArea = new JTextArea();
		jTextArea.setBounds(180, 40, 400, 250);
		jTextArea.setLineWrap(true);
		this.add(jTextArea);
		buttonGroup.add(buttonA);
		buttonGroup.add(buttonB);
		buttonGroup.add(buttonC);
		buttonGroup.add(buttonD);
		this.add(jLabel);
		setActionListenerForThis(buttonOK);
		setActionListenerForThis(buttonA);
		setActionListenerForThis(buttonB);
		setActionListenerForThis(buttonC);
		setActionListenerForThis(buttonD);
		initExamples();
		refreshDisplayMessage();
	}

	private void initExamples() {
		ExampleManager manager = new ExampleManager();
		manager.initExample();
		example = manager.genrateExample(15);

	}
}
